﻿using System;
using System.IO;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Components.Helpers;

namespace ASPNETVN.PORTAL.Components.Share.Controls
{
    public partial class UploadFile : AddEditItemControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                if (this.Settings["StorageType"].ToString().ToUpper().Equals("LINK"))
                {
                    TableUploadLinkFile.Visible = true;
                    TableUploadFile.Visible = false;
                }
                else
                {
                    TableUploadLinkFile.Visible = false;
                    TableUploadFile.Visible = true;
                }
            }
        }

        protected void DataListAttachment_DeleteCommand(object source, DataListCommandEventArgs e)
        {
            int ret = Attachment.DeleteAttachment(Convert.ToInt32(DataListAttachment.DataKeys[e.Item.ItemIndex]));
            if (ret > 0)
            {
                if (this.Settings["StorageType"].ToString().ToUpper().Equals("FILE"))
                {
                    string filename = this.Server.MapPath("~/Uploads/" + this.ModuleID + "/" + e.CommandArgument);
                    if (File.Exists(filename))
                    {
                        File.Delete(filename);
                    }
                }
                DataListAttachment.DataBind();
            }
        }

        protected void btnFinish_Click(object sender, EventArgs e)
        {
            this.ReturnToReferrerPage();
        }

        protected void ButtonUpload_Click(object sender, EventArgs e)
        {
            // Lưu tập tin.
            string fileUrl = string.Empty;
            int fileSize = FileUploadAttachment.PostedFile.ContentLength;
            byte[] buffer = new byte[0];
            string storageType = this.Settings["StorageType"].ToString().ToUpper();

            if (storageType.Equals("DATABASE"))
            {
                Stream stream = FileUploadAttachment.PostedFile.InputStream;
                buffer = new byte[FileUploadAttachment.PostedFile.ContentLength];

                try
                {
                    stream.Read(buffer, 0, fileSize);
                }
                finally
                {
                    stream.Close();
                }
                fileUrl = "#";
            }
            else if (storageType.Equals("FILE"))
            {
                string physicalDirectory = Server.MapPath("~/Uploads/" + this.ModuleID);
                // Tạo thư mục để chứa tập tin.
                if (Directory.Exists(physicalDirectory) == false)
                {
                    Directory.CreateDirectory(physicalDirectory);
                }

                string physicalPath = physicalDirectory + "\\" + this.FileUploadAttachment.FileName;
                int i = 1;
                while (File.Exists(physicalPath))
                {
                    physicalPath = physicalDirectory + "\\" + i + "_" + this.FileUploadAttachment.FileName;
                    i++;
                }
                FileInfo fi = new FileInfo(physicalPath);
                FileUploadAttachment.SaveAs(physicalPath);
                fileUrl = "~/Uploads/" + this.ModuleID + "/" + fi.Name;
            }

            Attachment attachment = new Attachment();
            attachment.ModuleID = this.ModuleID;
            attachment.ItemID = this.ItemID;
            attachment.StorageType = storageType;
            attachment.FileSize = fileSize;
            attachment.FileName = FileUploadAttachment.FileName;
            attachment.FileUrl = fileUrl;
            attachment.ContentType = FileUploadAttachment.PostedFile.ContentType;
            attachment.BinaryContent = buffer;
            int attachmentID = attachment.Insert();

            if (storageType.Equals("DATABASE"))
            {

                attachment.FileUrl = HttpUrlBuilder.BuildShareUrl(this.ModuleID, attachmentID, "DownloadFile");
                attachment.Update();
            }

            // Refresh DataList.
            DataListAttachment.DataBind();
        }

        protected void ButtonUploadLinkFile_Click(object sender, EventArgs e)
        {
            var attachment = new Attachment();
            attachment.ModuleID = this.ModuleID;
            attachment.ItemID = this.ItemID;
            attachment.FileUrl = txtUrl.Text;
            long filesize;
            switch (cbFileSize.SelectedValue)
            {
                case "KB":
                    filesize = Convert.ToInt64(Convert.ToDecimal(txtFileSize.Text) * 1024);
                    break;
                case "MB":
                    filesize = Convert.ToInt64(Convert.ToDecimal(txtFileSize.Text) * 1024 * 1024);
                    break;
                default:
                    filesize = Convert.ToInt64(Convert.ToDecimal(txtFileSize.Text) * 1024 * 1024 * 1024);
                    break;

            }
            attachment.FileSize = filesize;
            attachment.ContentType = txtFileType.Text.ToLower();
            attachment.StorageType = AttachmentStorageType.LINK.ToString();
            attachment.FileName = txtFileName.Text;
            attachment.Insert();

            // Refresh DataList.
            DataListAttachment.DataBind();
        }
    }
}